home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / jam / jamdisk4 / arptools / src / detab.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  245 lines

  1. /*
  2.           DeTab - Convert TABs into blanks.
  3.  
  4.           Based on the detab program by Gary Brant found on <>< 179.
  5.           ARP and Resident support by Fabio Rossetti.
  6.  
  7.           (c) 1989 by Fabio Rossetti
  8.  
  9.           To compile under Lattice C v5.0x use:
  10.  
  11.         lc -O -v -cus detab
  12.         blink lib:cres.o detab.o to detab lib lib:a.lib lib:lc.lib sd nd
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/dosextens.h>
  21. #include <libraries/arpbase.h>
  22. #include <arpfunctions.h>
  23.  
  24. #include <proto/exec.h>
  25. #include <proto/dos.h>        
  26.  
  27. struct ArpBase *ArpBase=NULL;
  28. struct Process *Pr;
  29. LONG argc;
  30.  
  31. #define NARGS 3
  32. #define BFSIZE 256    
  33. #define FROM    argv[0]
  34. #define TO    argv[1]
  35. #define TAB    argv[2]
  36.  
  37. STRPTR argv[NARGS];
  38.  
  39. struct UserAnchor {
  40.     struct    AnchorPath    ua_AP;
  41.     BYTE    moremem[255];    /* extension */
  42. };
  43. struct    UserAnchor *Anchor=NULL;
  44.  
  45. /* Trick to keep code down to size */
  46. VOID MemCleanup()
  47. {
  48. }
  49.  
  50. /* shutdown routine */
  51. VOID Cleanup(r1,r2,msg)
  52. LONG r1,r2;
  53. STRPTR msg;
  54. {
  55.     if (msg) Puts(msg);
  56.     if (ArpBase) CloseLibrary((struct Library *)ArpBase);
  57.     Pr->pr_Result2 = r2;
  58.     exit(r1);
  59. }
  60.  
  61. #define MAXLINE 256
  62.  
  63. char tabarray[MAXLINE];
  64.  
  65.  
  66. VOID _main(Line)
  67. STRPTR Line;
  68.  
  69. {
  70.  
  71.    int i = 0, j, k, l, ntabs = 0, tabstop = 8;
  72.    int argtabs[99];
  73.    char ch;
  74.    char **Argv;
  75.     BPTR fh,ofh;
  76.  
  77.  
  78.  
  79.     Pr = (struct Process*)FindTask(NULL);
  80.     
  81.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion)))
  82.             Cleanup(RETURN_FAIL,ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  83.     
  84.     /* parse command line */
  85.     for (argc=0;argc < NARGS ;++argc)
  86.         argv[argc] = (STRPTR) NULL;
  87.  
  88.     while(*Line > ' ')
  89.         ++Line;
  90.  
  91.     if((argc = GADS(++Line,
  92.         strlen(Line),
  93.         "Usage: DeTab [FROM Pattern] [TO Filename] [TAB tabsize [tabsize ..] ]",
  94.         argv,
  95.         "FROM,TO,TAB/..." )) < 0)
  96.             Cleanup(RETURN_WARN,NULL,FROM);
  97.  
  98.  
  99.    /* decode tabstop arguments */
  100.    Argv = (char **)TAB;
  101.    while ((Argv[i] != NULL)) {
  102.       j = 0;
  103.       tabstop = 0;
  104.       while ((ch = (char)Argv[i++][j++]) != '\0') {
  105.      if (ch >= '0' && ch <= '9') {
  106.         tabstop *= 10;
  107.         tabstop += ch - '0';
  108.      } else
  109.         Cleanup(RETURN_WARN,NULL,"Bad args");
  110.       }
  111.       argtabs[ntabs++] = tabstop;
  112.    }
  113.  
  114.    /* fill tabarray with \1 for each tabstop position */
  115.    for (k = 0; k < MAXLINE; k++)
  116.       tabarray[k] = '\0';
  117.    if (ntabs > 1)
  118.       for (k = 0; k < ntabs; k++)
  119.      if ((l = argtabs[k]-1) < MAXLINE)
  120.         tabarray[l] = '\001';
  121.      else
  122.         Cleanup(RETURN_WARN,NULL,"Bad tab specification");
  123.    else if ((tabstop > 0) && (tabstop < MAXLINE))
  124.       for (k = tabstop; k < MAXLINE; k += tabstop)
  125.      tabarray[k] = '\001';
  126.    else Cleanup(RETURN_WARN,NULL,"Bad tab specification");
  127.  
  128.  
  129.     if ( Anchor = (struct UserAnchor *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  130.     {
  131.         Anchor->ua_AP.ap_Length = 255;    /* Want full path built */
  132.         
  133.     }
  134.     else Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error:No memory");
  135.     
  136.     if (FROM)  {
  137.  
  138.     if (!(FindFirst(FROM,(struct AnchorPath*) Anchor)))
  139.         if (Anchor->ua_AP.ap_Info.fib_DirEntryType < 0) {
  140.             if(!(fh=ArpOpen(Anchor->ua_AP.ap_Buf,MODE_OLDFILE))) {
  141.                 Printf("Can't open %s\n",FROM);
  142.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  143.                 }
  144.             }
  145.         else {
  146.             Printf("%s is a directory !\n",TO);
  147.             Cleanup(RETURN_ERROR,ERROR_OBJECT_WRONG_TYPE,NULL);
  148.             }
  149.         else {
  150.             Printf("Can't find %s\n",FROM);
  151.             Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  152.             }
  153.         }
  154.     else fh = Input();
  155.  
  156.     if(TO) {
  157.             if(!(ofh=ArpOpen(TO,MODE_NEWFILE))) {
  158.                 Printf("Can't open %s\n",TO);
  159.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  160.                 }
  161.             }
  162.     else ofh = Output();
  163.  
  164.         detab (fh,ofh);
  165.  
  166.     Cleanup(NULL,NULL,NULL);
  167.  
  168. }
  169.  
  170.  
  171. /* detab - remove the tabs from one file */
  172.  
  173. detab (ifile,ofile)
  174. BPTR ifile,ofile;
  175. {
  176.    int n;
  177.    char inline[MAXLINE],Buf[MAXLINE], outline[MAXLINE];
  178.  
  179. register int count, obp=0;
  180. register char *il = inline;
  181.  
  182. /* read lines */
  183. for (;;) {
  184. if (!obp) if (!(count = Read(ifile,Buf,BFSIZE))) {        
  185.             return(0);
  186.             }
  187.  
  188. if (*(Buf + obp) == '\n') {
  189.         ++obp;
  190.         *il = '\0';
  191.         il = inline;
  192.  
  193.         if (SetSignal(0,0) & SIGBREAKF_CTRL_C) 
  194.         Cleanup(RETURN_WARN,NULL,"***Break");
  195.  
  196.  
  197.       n = strlen (inline);
  198.       while (--n >= 0)            /* back over white space */
  199.      if (inline[n] != ' ' && inline[n] != '\t' &&
  200.         inline[n] != '\n') break;
  201.       inline[n+1] = '\0';
  202.       expand (inline, outline, MAXLINE);
  203.       FPrintf(ofile,"%s\n",outline);
  204.    }
  205. else {
  206.     if (((char *)il - (char *)inline) > MAXLINE) 
  207.             Cleanup(RETURN_FAIL,NULL,"Buffer overflow");
  208.     *il++ = *(Buf + obp++);
  209.     }
  210. if (obp == count) obp = 0;
  211. }
  212. }
  213.  
  214.  
  215. /* expand - expand a line */
  216.  
  217. expand (in, out, lim)
  218. char in[], out[];
  219. int  lim;
  220. {
  221.    register int i, j;
  222.    register char ch;
  223.  
  224.    i = j = 0;
  225.    while (((ch = in[i++]) != '\0') && (j < lim)) {
  226.       if (ch == '\t') {
  227.      register int k;
  228.      register char tc;
  229.  
  230.      k = j;
  231.      out[j++] = ' ';
  232.      while ((j < lim) && ((tc = tabarray[j]) == '\0'))
  233.         out[j++] = ' ';
  234.      if (tc == '\0') {
  235.         j = k;
  236.         out[j++] = ch;
  237.      }
  238.       } else
  239.      out[j++] = ch;
  240.    }
  241.    out[j] = ch;
  242. }
  243.  
  244.  
  245.